/* $Id: SQLDbTypeConverter.cs,v 1.4 2004/11/29 10:45:37 larsbm Exp $
 * Copyright (c) 2004 Engine EAR GmbH & Co. KG
 * Developed by: Lars Behrmann, lb@engine.de
 */

// Taken from neo norque.dtd
// represents possible datatypes for columns resp. object properties
//	type
//	(
//	BIT  | TINYINT | SMALLINT    | INTEGER    | BIGINT    | FLOAT
//	| REAL | NUMERIC | DECIMAL     | CHAR       | VARCHAR   | LONGVARCHAR
//	| DATE | TIME    | TIMESTAMP   | BINARY     | VARBINARY | LONGVARBINARY
//	| NULL | OTHER   | JAVA_OBJECT | DISTINCT   | STRUCT    | ARRAY
//	| BLOB | CLOB    | REF         | BOOLEANINT | BOOLEANCHAR | UNIQUEIDENTIFIER
//	| DOUBLE
//	) "VARCHAR"

using System;
using System.Collections;
using System.Data.OleDb;

namespace SQLToNeo.Model
{
	/// <summary>
	/// SQLDbTypeConverter
	/// </summary>
	public class SQLDbTypeConverter : IDBTypeConverter
	{
		private Hashtable _dbtypes;
		private string _name;
		private String[] _typeArray = new String[206];

		public SQLDbTypeConverter()
		{
			_dbtypes = new Hashtable();
			_name = "SQL Datatype Converter";
			InitTypes();
			NewTypes();
		}

		private void NewTypes()
		{
			_typeArray[0] = null;    //  adEmpty
			_typeArray[2] = "SMALLINT";    //  adSmallInt
			_typeArray[3] = "INTEGER";    //  adInteger
			_typeArray[4] = "REAL";    //  adSingle
			_typeArray[5] = "FLOAT";    //  adDouble
			_typeArray[6] = "MONEY";    //  adCurrency
			_typeArray[7] = null;    //  adDate
			_typeArray[8] = null;    //  adBSTR
			_typeArray[9] = null;    //  adIDispatch
			_typeArray[10] = null;    //  adError
			_typeArray[11] = "BIT";    //  adBoolean
			_typeArray[12] = "SQL_VARIANT";    //  adVariant
			_typeArray[13] = null;    //  adIUnknown
			_typeArray[14] = null;    //  adDecimal
			_typeArray[16] = null;    //  adTinyInt
			_typeArray[17] = "TINYINT";    //  adUnsignedTinyInt
			_typeArray[18] = null;    //  adUnsignedSmallInt
			_typeArray[19] = null;    //  adUnsignedInt
			_typeArray[20] = "BIGINT";    //  adBigInt
			_typeArray[21] = null;    //  adUnsignedBigInt
			_typeArray[64] = null;    //  adFileTime
			_typeArray[72] = "UNIQUEIDENTIFIER";    //  adGUID
			_typeArray[128] = "BINARY";    //  adBinary
			_typeArray[129] = "CHAR";    //  adChar
			_typeArray[130] = "NCHAR";    //  adWChar
			_typeArray[131] = "DECIMAL";    //  adNumeric
			_typeArray[132] = null;    //  adUserDefined
			_typeArray[133] = null;    //  adDBDate
			_typeArray[134] = null;    //  adDBTime
			_typeArray[135] = "DATETIME";    //  adDBTimeStamp
			_typeArray[136] = null;    //  adChapter
			_typeArray[137] = null;    //  adDBFileTime
			_typeArray[138] = null;    //  adPropVariant
			_typeArray[139] = null;    //  adVarNumeric
			_typeArray[200] = "VARCHAR";    //  adVarChar
			_typeArray[201] = "TEXT";    //  adLongVarChar
			_typeArray[202] = "NVARCHAR";    //  adVarWChar
			_typeArray[203] = "NTEXT";    //  adLongVarWChar
			_typeArray[204] = "VARBINARY";    //  adVarBinary
			_typeArray[205] = "IMAGE";    //  adLongVarBinary
		}

		private void InitTypes()
		{
			DBTypes.Add("guid","UNIQUEIDENTIFIER");
			DBTypes.Add("dbtimestamp","DATE");
			DBTypes.Add("char","CHAR");
			DBTypes.Add("integer","INTEGER");
			DBTypes.Add("int","INTEGER");
			DBTypes.Add("bigint","INTEGER");
			DBTypes.Add("boolean","BIT");
			DBTypes.Add("bit","BIT");
			DBTypes.Add("numeric","REAL");
			DBTypes.Add("single","REAL");
			DBTypes.Add("double","REAL");
			DBTypes.Add("binary","BINARY");
			DBTypes.Add("varchar","VARCHAR");
			DBTypes.Add("wchar","CHAR");
			DBTypes.Add("decimal","REAL");
			DBTypes.Add("currency","REAL");
			DBTypes.Add("real","REAL");
			DBTypes.Add("money","REAL");
			DBTypes.Add("smallint","INTEGER");
			DBTypes.Add("tinyint","INTEGER");
			DBTypes.Add("datetime","DATETIME");
			DBTypes.Add("smalldatetime","DATETIME");
			DBTypes.Add("image","IMAGE");
			DBTypes.Add("text","TEXT");
			DBTypes.Add("nvarchar","VARCHAR");
			DBTypes.Add("ntext","TEXT");
			DBTypes.Add("nchar","CHAR");
			DBTypes.Add("uniqueidentifier","UNIQUEIDENTIFIER");
			DBTypes.Add("float","FLOAT");
			DBTypes.Add("varbinary","VARBINARY");
			//TODO: PRIO 2: Check out, if there are more types needed
			// and if these type converts above works fine
		}

		#region IDBTypeConverter Member

		public System.Collections.Hashtable DBTypes
		{
			get
			{
				return _dbtypes;
			}
			set
			{
				_dbtypes = value;
			}
		}

		public string ConvertName
		{
			get
			{
				return _name;
			}
			set
			{
				
			}
		}

		public string Convert(string dbtype)
		{
			if(DBTypes.ContainsKey(dbtype.ToLower()))
			{
				Console.WriteLine("Mapped " + dbtype.ToLower() + " to " + DBTypes[dbtype.ToLower()].ToString());
				return DBTypes[dbtype.ToLower()].ToString();
			}
			else
			{
				Console.WriteLine("\tUnable to map " + dbtype.ToLower() + " to " + DBTypes[dbtype.ToLower()]);
				throw new System.ApplicationException("Unhandled SQL Data Type: " + dbtype);
			}
			//return "VARCHAR";
		}

		public string Convert(int OleDbId)
		{
			Console.WriteLine("Mapped " + ((OleDbType) OleDbId).ToString() + " (" + OleDbId.ToString() + ") " + " to " + _typeArray[OleDbId]);
			return _typeArray[OleDbId];
		}

		#endregion
	}
}

/*
 * $Log: SQLDbTypeConverter.cs,v $
 * Revision 1.4  2004/11/29 10:45:37  larsbm
 * - bugfix write ireference instead reference
 * - bugfix hidden was always true
 * - conversion to boolean failed
 * - set readonlys
 *
 * Revision 1.3  2004/11/08 09:56:02  larsbm
 * - set numeric, singel, double to REAL
 *
 * Revision 1.2  2004/10/27 14:09:23  larsbm
 * - Fixed bugs in tablebuilder, TablestyleCreator
 * - Working on the forms
 * - Finishing DBTypes
 * - Add IWriter & Writer, NeoXMLModelWriter as abstraction for writing the xml model
 *
 * Revision 1.1  2004/10/26 13:23:45  larsbm
 * - Fixed bugs in tablebuilder
 * - Working on the forms
 * - Finishing MergeSpecial
 * - Make app run with mergespecial
 * - start converter for sqldatatypes
 *
 */